$(function() {
    //主流程对象
    var mainCtrl = avalon.define({
        $id: "mainController",
        taskEndTime: "",
        taskStartTime: "",
        taskType: "",
        operateTaskId: "", //删除、启用、禁用时需要用到的任务id
        taskStatus: "", //0 未开始 1 进行用 2 已结束 99已禁用，空即全部(查询状态)
        search: function() {
            var searchParam = {
                "taskStartTime": mainCtrl.taskStartTime,
                "taskEndTime": mainCtrl.taskEndTime,
                "pageNo": 1,
                "pageSize": 10,
                "taskType": mainCtrl.taskType,
                "taskStatus": mainCtrl.taskStatus
            }
            taskTable.search(searchParam);
        },
        delPop: function(taskid) {
            popCtrl.confirmType = "del";
            mainCtrl.operateTaskId = taskid;
            $("#popup_confirm").show();
        },
        enablePop: function(row) {
            //0禁用 ，1启用；所以0的时候操作就为去启用，1的时候操作为去禁用
            if (row.validStatus == 1) {
                popCtrl.confirmType = "disable";
            } else {
                popCtrl.confirmType = "enable";
            }
            mainCtrl.operateTaskId = row.taskId;
            $("#popup_confirm").show();
        },
        addTask: function(){
            window.location = "editTask.html?pageType=2";
        }
    });
    //表格实例化
    var taskTable = null;
    var opts = {
        targetId: "taskTable",
        templateId: "tableTask",
        settings: {
            url: DI.queryModelTask,
            ajaxType: "json",
            sourceInterface: {
                INum: "totalSize",
                IData: "records"
            },
            paginationIndexName: {
                "peerpageRows": "pageSize",
                "currentPage": "pageNo"
            },
            searchParam: {
                "taskStartTime": mainCtrl.taskStartTime,
                "taskEndTime": mainCtrl.taskEndTime,
                "pageNo": 1,
                "pageSize": 10,
                "taskType": mainCtrl.taskType,
                "taskStatus": mainCtrl.taskStatus
            }
        }
    };
    taskTable = SwiftUI.swiftTable.getInstance(opts);

    //弹框对象
    var popCtrl = avalon.define({
        $id: "popController",
        confirmType: "del",
        confirmSure: function() {
            if (popCtrl.confirmType == "del") {
                delTask();
                return;
            }
            doEnable();
        }
    });
    //删除任务
    var delTask = function() {
        $.ajax({
            type: "post",
            dataType: "json",
            data: {
                "taskId": mainCtrl.operateTaskId
            },
            url: DI.delmodelTask,
            success: function(data) {
                $("#popup_confirm").hide();
                if (data.retFlag == 1) {
                    PLATFORM.showTips("success", "删除成功");
                    taskTable.searchParam.pageNo = "1";
                    taskTable.refresh();
                } else {
                    PLATFORM.showTips("error", data.errorMessage);
                }
            },
            error: function(err) {
                console.log(err);
            }
        });
    }
    //启用、禁用 任务
    var doEnable = function() {
        var operateType = 0;
        if (popCtrl.confirmType == "enable") {
            operateType = 1;
        }
        $.ajax({
            type: "post",
            dataType: "json",
            data: {
                "taskId": mainCtrl.operateTaskId,
                "operateType":operateType
            },
            url: DI.validTask,
            success: function(data) {
                $("#popup_confirm").hide();
                if (data.retFlag == 1) {
                    PLATFORM.showTips("success", operateType==0?"禁用成功":"启用成功");
                    taskTable.searchParam.pageNo = "1";
                    taskTable.refresh();
                } else {
                    PLATFORM.showTips("error", data.errorMessage);
                }
            },
            error: function(err) {
                console.log(err);
            }
        });
    }

    //实例化时间
    PLATFORM.datepickerRange("#starttime", "#endtime");

    avalon.scan();
});
/*
 * 任务状态 过滤器
 * 0 未开始 ,1 进行中,2 已结束,3 已删除;
 */
avalon.filters.filterTaskStatus = function(str, args, args2) {
    var ret = "未开始";
    if (str == 0) {
        ret = "未开始"
    } else if (str == 1) {
        ret = "进行中";
    } else if (str == 2) {
        ret = "已结束";
    } else if (str == 3) {
        ret = "已删除";
    }
    return ret;
};